/*
* ====================================================================
* Copyright (c) 2013-2014 Neudesic.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that redistributions of source
* retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* This source file includes software written by Neudesic, LLC (http://www.neudesic.com).
*
* THIS SOFTWARE IS PROVIDED BY Neudesic, LLC ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  FURTHER, IN NO EVENT SHALL
* Neudesic, LLC (THE AUTHOR) OR ANY CONTRIBUTORS BE LIABLE FOR ANY DAMAGES
* WHATSOEVER THAT ARE RELATED TO THE SOFTWARE [INCLUDING BUT NOT LIMITED TO,
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* SUCH AS LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION, HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)] EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* ====================================================================
*/
#include "HandleManager.h"

namespace Neudesic {
	namespace Sockets {
		HandleManager::HandleManager() : handle(0)
		{
		}

		HandleManager::HandleManager(HANDLE handle)
		{
			this->handle = handle;
		}

		HandleManager::~HandleManager()
		{
			this->Close();
		}

		void HandleManager::Attach(HANDLE handle)
		{
			this->handle = handle;
		}

		HANDLE HandleManager::Detach()
		{
			HANDLE h = this->handle;
			this->handle = 0;
			return h;
		}

		HANDLE HandleManager::Get() const
		{
			return this->handle;
		}

		int HandleManager::Close()
		{
			HANDLE h = this->handle;
			this->handle = 0;
			return h ? ::CloseHandle(h) : 1;
		}

		void EventManager::Set()
		{
			::SetEvent(this->Get());
		}

		void EventManager::Reset()
		{
			::ResetEvent(this->Get());
		}

		int EventManager::Wait(DWORD timeout) const
		{
			return ::WaitForSingleObjectEx(this->Get(), timeout, false);
		}

		int EventManager::WaitAll(DWORD count, const HANDLE* lpHandles, BOOL bWaitAll, DWORD timeout)
		{
			return ::WaitForMultipleObjectsEx(count, lpHandles, bWaitAll, timeout, false);
		}

		void EventManager::CreateEvent(DWORD dwFlags, LPSECURITY_ATTRIBUTES lpEventAttributes, LPCTSTR lpName)
		{
			HANDLE h = ::CreateEventEx(lpEventAttributes, lpName, dwFlags, EVENT_ALL_ACCESS);
			this->Attach(h);
		}

		ManualResetEvent::ManualResetEvent()
		{
			this->CreateEvent(CREATE_EVENT_MANUAL_RESET, NULL, NULL);
		}

		ManualResetEvent::ManualResetEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCTSTR lpName)
		{
			this->CreateEvent(CREATE_EVENT_MANUAL_RESET, lpEventAttributes, lpName);
		}


		AutoResetEvent::AutoResetEvent()
		{
			this->CreateEvent(0, NULL, NULL);
		}

		AutoResetEvent::AutoResetEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCTSTR lpName)
		{
			this->CreateEvent(0, lpEventAttributes, lpName);
		}
	}
}